﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="Silverlight.js"/>
/// <reference path="aaagctrl.debug.js"/>
/// <reference path="SilverlightControlToolkit.debug.js"/>

MySLManager= function(host) {
    MySLManager.initializeBase(this, [ host ]);
    this._debugBox = null;
}
/******************************************************************** */
 /*Animation templates */
/******************************************************************** */

// Buttons

MySLManager.mouseEnterTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_textBlock\" Storyboard.TargetProperty=\"(TextBlock.Foreground).(SolidColorBrush.Color)\"><SplineColorKeyFrame KeyTime=\"00:00:00.6000000\" Value=\"#FF00FF00\"/></ColorAnimationUsingKeyFrames></Storyboard>";
MySLManager.mouseLeaveTemplate = "<Storyboard  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_textBlock\" Storyboard.TargetProperty=\"(TextBlock.Foreground).(SolidColorBrush.Color)\"><SplineColorKeyFrame KeyTime=\"00:00:00.6000000\" Value=\"#FF000000\"/></ColorAnimationUsingKeyFrames></Storyboard>";
MySLManager.mouseDownTemplate = "<Storyboard  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"  x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangle\" Storyboard.TargetProperty=\"(Shape.Fill).(SolidColorBrush.Color)\"><SplineColorKeyFrame KeyTime=\"00:00:00.1000000\" Value=\"#FFF2FF00\"/></ColorAnimationUsingKeyFrames></Storyboard>";
MySLManager.mouseUpTemplate = "<Storyboard  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"  x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangle\" Storyboard.TargetProperty=\"(Shape.Fill).(SolidColorBrush.Color)\"><SplineColorKeyFrame KeyTime=\"00:00:00.1000000\" Value=\"#FFFF0000\"/></ColorAnimationUsingKeyFrames></Storyboard>";

// Radios

MySLManager.radioEnterTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellipseOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\"><SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF4E6D78\"/></ColorAnimationUsingKeyFrames><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">				<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF6F9EAF\"/>			</ColorAnimationUsingKeyFrames>            <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">            <SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF8E9DC7\"/>            </ColorAnimationUsingKeyFrames>            </Storyboard>";
MySLManager.radioLeaveTemplate="<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellipseOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">			<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF767676\"/>			</ColorAnimationUsingKeyFrames>              <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">			<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFADADAD\"/>			</ColorAnimationUsingKeyFrames>              <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">              <SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFC8C8C8\"/>              </ColorAnimationUsingKeyFrames>              </Storyboard>";
MySLManager.radioDownTemplate="<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\"><SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"1\"/></DoubleAnimationUsingKeyFrames></Storyboard>";
MySLManager.radioUpTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_ellispeSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\"><SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"0\"/></DoubleAnimationUsingKeyFrames></Storyboard>";

// Checkboxes : 

MySLManager.checkEnterTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">            	<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF4E6D78\"/>            </ColorAnimationUsingKeyFrames>            <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">            	<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF6F9EAF\"/>            </ColorAnimationUsingKeyFrames>            <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">			<SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF8E9DC7\"/>            </ColorAnimationUsingKeyFrames>            </Storyboard>";
MySLManager.checkLeaveTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleOuter\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">            <SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FF767676\"/>            </ColorAnimationUsingKeyFrames>            <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Stroke).(SolidColorBrush.Color)\">            <SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFADADAD\"/>            </ColorAnimationUsingKeyFrames>            <ColorAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_rectangleInner\" Storyboard.TargetProperty=\"(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)\">            <SplineColorKeyFrame KeyTime=\"00:00:00.3000000\" Value=\"#FFC8C8C8\"/>            </ColorAnimationUsingKeyFrames>            </Storyboard>";
MySLManager.checkDownTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_pathSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\"><SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"1\"/></DoubleAnimationUsingKeyFrames></Storyboard>";
MySLManager.checkUpTemplate = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"$name$\"><DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"$targetname$_pathSelector\" Storyboard.TargetProperty=\"(UIElement.Opacity)\"><SplineDoubleKeyFrame KeyTime=\"00:00:00.2000000\" Value=\"0\"/></DoubleAnimationUsingKeyFrames></Storyboard>";

/******************************************************************** */
/* component behavior */
/******************************************************************** */

MySLManager.prototype = 
{
    onSilverlightLoaded:function( slControl, args)
    {
        var p = this.get_plugin();
        this._debugBox = p.content.findName("txtDebug");
        
        // Initialise delegates
        
        var onClickHandler = Function.createDelegate(this, this.onClick);
        var onPushedHandler = Function.createDelegate(this,this.onPushed);
        var onUnPushedHandler = Function.createDelegate(this,this.onUnPushed);
        var onSelectionChangedHandler = Function.createDelegate(this, this.onSelectionChanged);
        
        // Create buttons (using animation templates)
        
        var btn0 = $create(SilverlightControlToolkit.Button, {plugin:p,
                                elementName:"MyButton", 
                                mouseEnterTemplate : MySLManager.mouseEnterTemplate,
                                mouseLeaveTemplate : MySLManager.mouseLeaveTemplate,
                                mouseDownTemplate : MySLManager.mouseDownTemplate,
                                mouseUpTemplate: MySLManager.mouseUpTemplate
                                }, 
                                {click:onClickHandler},
                                null,null);
      var btn1 = $create(SilverlightControlToolkit.Button, {plugin:p,
                                elementName:"MyButton1", 
                                mouseEnterTemplate : MySLManager.mouseEnterTemplate,
                                mouseLeaveTemplate : MySLManager.mouseLeaveTemplate,
                                mouseDownTemplate : MySLManager.mouseDownTemplate,
                                mouseUpTemplate: MySLManager.mouseUpTemplate
                                }, 
                                {click:onClickHandler},
                                null,null);
      var btn2 = $create(SilverlightControlToolkit.Button, {plugin:p,
                                elementName:"MyButton2", 
                                mouseEnterTemplate : MySLManager.mouseEnterTemplate,
                                mouseLeaveTemplate : MySLManager.mouseLeaveTemplate,
                                mouseDownTemplate : MySLManager.mouseDownTemplate,
                                mouseUpTemplate: MySLManager.mouseUpTemplate
                                }, 
                                {click:onClickHandler},
                                null,null);                                
                                
      // Create a single toggle button
      
      var toggleBtn = $create(SilverlightControlToolkit.ToggleButton, {plugin:p,
                                elementName:"ToggleButton", 
                                mouseEnterTemplate : MySLManager.mouseEnterTemplate,
                                mouseLeaveTemplate : MySLManager.mouseLeaveTemplate,
                                mouseDownTemplate : MySLManager.mouseDownTemplate,
                                mouseUpTemplate: MySLManager.mouseUpTemplate
                                }, 
                                {click:onClickHandler,
                                pushed:onPushedHandler,
                                unpushed:onUnPushedHandler},
                                null,null);                   
                                
       // Create a RadioButtonList (ToggleButtonList with "radioButtonList" behavior
       // filled with an array of ToggleButtons
       var radioBtns = new Array();
       for(var i = 0; i<4;i++)
       {
            var radio = $create(SilverlightControlToolkit.ToggleButton,
                        {   plugin:p,
                            elementName:"Radio"+i.toString(), 
                            mouseEnterTemplate : MySLManager.radioEnterTemplate,
                            mouseLeaveTemplate : MySLManager.radioLeaveTemplate,
                            mouseDownTemplate : MySLManager.radioDownTemplate,
                            mouseUpTemplate: MySLManager.radioUpTemplate
                        }, null,null,null);
             Array.add(radioBtns,radio);
       }          
       var radioBtnList = $create(SilverlightControlToolkit.ToggleButtonList,
            { plugin:p,
               items:radioBtns,
               behavior:SilverlightControlToolkit.ToggleButtonListBehavior.radioButtonList,
               id:"radioButtonList"},
            {
                selectionChanged:onSelectionChangedHandler
            },null,null);              
                                
        // Create a CheckboxList (ToggleButtonList with "checkboxButtonList" behavior
       // filled with an array of ToggleButtons                        
       var checkBtns = new Array();
       for(var i = 0; i<4;i++)
       {
            var check = $create(SilverlightControlToolkit.ToggleButton,
                        {   plugin:p,
                            elementName:"Check"+i.toString(), 
                            mouseEnterTemplate : MySLManager.checkEnterTemplate,
                            mouseLeaveTemplate : MySLManager.checkLeaveTemplate,
                            mouseDownTemplate : MySLManager.checkDownTemplate,
                            mouseUpTemplate: MySLManager.checkUpTemplate
                        }, null,null,null);
             Array.add(checkBtns,check);
       }          
       var checkBtnList = $create(SilverlightControlToolkit.ToggleButtonList,
            { plugin:p,
               items:checkBtns,
               behavior:SilverlightControlToolkit.ToggleButtonListBehavior.checkboxButtonList,
               id:"checkboxButtonList"},
            {
                selectionChanged:onSelectionChangedHandler
            },null,null);     
        
        //  create a StackPanel       
        var stackPanel = $create(SilverlightControlToolkit.StackPanel,
        {plugin:p,
        elementName:"StackPanelTest"},null,null,null);

        // Create Behavior
        //var frect1 = $create(SilverlightControlToolkit.AgBehavior,
        //        {plugin:p,
        //        elementName:"FlowRect1"},null,null,null); 
        //var frect2 = $create(SilverlightControlToolkit.AgBehavior,
        //        {plugin:p,
        //        elementName:"FlowRect2"},null,null,null); 
        //var ftext1 = $create(SilverlightControlToolkit.AgBehavior,
        //        {plugin:p,
        //        elementName:"FlowText1"},null,null,null); 
        //var ftext2 = $create(SilverlightControlToolkit.AgBehavior,
        //        {plugin:p,
        //        elementName:"FlowText2"},null,null,null); 
        //  create a FlowPanel       
        var flowPanel = $create(SilverlightControlToolkit.FlowPanel,
        {plugin:p,
        elementName:"FlowPanelTest"},null,null,null);
        
        /*************************/
        /* Grid layout manager */
        /*************************/
        
        // First, prepare the children (Grid only supports children with an attached behavior)
        
        var bg = $create(SilverlightControlToolkit.AgBehavior,
                {plugin:p,
                elementName:"GdBg"},null,null,null); // default grid layout is Stretch
        
        var center = $create(SilverlightControlToolkit.AgBehavior,
                {plugin:p,
                elementName:"GdCenter"},null,null,null);
        center.setNamedValue(SilverlightControlToolkit.Grid.horizontalAlignment, SilverlightControlToolkit.HorizontalAlignment.center);
        center.setNamedValue(SilverlightControlToolkit.Grid.verticalAlignment, SilverlightControlToolkit.VerticalAlignment.center);
              
        var top = $create(SilverlightControlToolkit.AgBehavior,
                {plugin:p,
                elementName:"GdTop"},null,null,null);
        top.setNamedValue(SilverlightControlToolkit.Grid.horizontalAlignment, SilverlightControlToolkit.HorizontalAlignment.center);
        top.setNamedValue(SilverlightControlToolkit.Grid.verticalAlignment, SilverlightControlToolkit.VerticalAlignment.top);
                
        var bottom = $create(SilverlightControlToolkit.AgBehavior,
                {plugin:p,
                elementName:"GdBottom"},null,null,null);
        bottom.setNamedValue(SilverlightControlToolkit.Grid.horizontalAlignment, SilverlightControlToolkit.HorizontalAlignment.center);
        bottom.setNamedValue(SilverlightControlToolkit.Grid.verticalAlignment, SilverlightControlToolkit.VerticalAlignment.bottom);
        
        var left = $create(SilverlightControlToolkit.AgBehavior,
                {plugin:p,
                elementName:"GdLeft"},null,null,null);
        left.setNamedValue(SilverlightControlToolkit.Grid.horizontalAlignment, SilverlightControlToolkit.HorizontalAlignment.left);
        left.setNamedValue(SilverlightControlToolkit.Grid.verticalAlignment, SilverlightControlToolkit.VerticalAlignment.center);
         
        var right = $create(SilverlightControlToolkit.AgBehavior,
                {plugin:p,
                elementName:"GdRight"},null,null,null);
        right.setNamedValue(SilverlightControlToolkit.Grid.horizontalAlignment, SilverlightControlToolkit.HorizontalAlignment.right);
        right.setNamedValue(SilverlightControlToolkit.Grid.verticalAlignment, SilverlightControlToolkit.VerticalAlignment.center);
                
        var grid = $create(SilverlightControlToolkit.Grid,
            {plugin:p,
            elementName:"GridSample"},
            null,null,null);
        this._debugBox.Text="Initialized !"             
    },
    onClick:function(sender,args)
    {
        this._debugBox.Text = sender.get_id();
    },
    onPushed:function(sender,args)
    {
        this._debugBox.Text = "Pushed!";
    },
    onUnPushed:function(sender,args)
    {
        this._debugBox.Text = "UnPushed!";
    },
    onSelectionChanged:function(sender, args)
    {
        var selection = sender.get_selectedItems();
        var txt = "new selection: ";
        for(var i=0;i<selection.length;i++)
        {
            txt+=selection[i].get_id()+"; ";
        }
        this._debugBox.Text=txt;
    }
}
MySLManager.registerClass("MySLManager", SilverlightControlToolkit.SLManager);